var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');

var app = getApp();

Page({
  data: {
    fenxianghuodong: {},
    fenxiangList:[],
    type: 0,
    typename: '活动中',
    page: 1,
    size: 10,
    scrollTop: 0
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    var that = this;
    that.getFenxianghuodong();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getFenxianghuodong: function () {
    let that = this;
    that.setData({
      scrollTop: 0,
      fenxianghuodong: {}
    });
    util.request(api.FengxiangyouliList, {
      type: that.data.type,
      page: that.data.page,
      size: that.data.size
    }).then(function (res) {
      if (res.result == 1) {
        if (res.data.fenxianghuodong.length > 0) {
          if (that.data.type == 0) {
            that.setData({
              scrollTop: 0,
              fenxianghuodong: res.data.fenxianghuodong[0]
            });
          }
          else {
            if (res.data.fenxianghuodong.length > 0) {
              that.setData({
                fenxiangList: that.data.fenxiangList.concat(res.data.fenxianghuodong),
                page: that.data.page + 1
              });
            } else {
              wx.showToast({
                title: '已经被您看完了!',
                icon: 'none',
                duration: 2000
              });
              return false;
            }
          }
        }
      }
      else{
        wx.showToast({
          title: '领取失败!',
          icon: 'none',
          duration: 2000
        });
        return false;
      }
    });
  },

  //切换页签
  switchTab: function (e) {
    let that = this;
    var type = e.currentTarget.dataset.type;
    that.setData({
      fenxianghuodong: {},
      fenxiangList: [],
      type: type,
      page: 1,
      size: 10,
      scrollTop: 0
    });
    if (type == 0) {
      that.setData({
        typename: '活动中'
      });
    }
    if (type == 1) {
      that.setData({
        typename: '已领取'
      });
    }
    if (type == 2) {
      that.setData({
        typename: '活动结束'
      });
    }
    that.getFenxianghuodong();
  },

  lingqu: function () {
    let that = this;
    if (that.data.fenxianghuodong.yifenxiang < that.data.fenxianghuodong.fenxiangnum) {
      wx.showToast({
        title: '分享次数还不够',
        icon: 'none',
        duration: 2000
      });
      return false;
    } else {
      util.jhxLoadShow("领取中...");
      let nowTime = +new Date();
      var lastTime = null;
      if (nowTime - lastTime > 5000 || !lastTime) { //5秒内避免重复提交订单
        lastTime = nowTime;
      } else {
        return false;
      }
      util.request(api.FengxiangHuodongLingqu, {
        id: that.data.fenxianghuodong.id,
        fenxiangnum: that.data.fenxianghuodong.fenxiangnum,
        yifenxiang: that.data.fenxianghuodong.yifenxiang
      }, 'POST').then(function (res) {
        if (res.result == 1) {
          util.jhxLoadShow("领取成功！");
          that.setData({
            fenxianghuodong: {}
          });
          util.jhxLoadHide();
        }
      });
    }
  }
})